﻿/*
 * AppVCard - templated control (see Themes/Generic.xaml for styles)
 * displays application VCard in AppMenu
 * 
 * created by: asida@centrum.cz
 * 
 * initial implementation: 8.12.2011
 * last updated:
 */
using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Net;
using System.Windows;
using System.Windows.Controls;
//using System.Windows.Documents;
//using System.Windows.Input;
using System.Windows.Media;
//using System.Windows.Media.Animation;
//using System.Windows.Shapes;

namespace uhul.Controls
{

  [TemplatePart(Name = appVCardLabel, Type = typeof(TextBlock))]
  [TemplatePart(Name = appVCardDescription, Type = typeof(TextBlock))]
  [TemplatePart(Name = appVCardIcon, Type = typeof(Image))]
  [TemplatePart(Name = appVCardProviderIcon, Type = typeof(Image))]
  [TemplatePart(Name = appIconLabelShadowColor, Type = typeof(RadialGradientBrush))]
  public class AppVCard : Button
  {

    //control names
    private const string appVCardLabel = "AppVCardLabel";
    private const string appVCardDescription = "AppVCardDescription";
    private const string appVCardIcon = "AppVCardIcon";
    private const string appVCardProviderIcon = "AppVCardProviderIcon";
    private const string appIconLabelShadowColor = "IconLabelShadowColor";


    //controls
    private TextBlock _vCardLabel;
    private TextBlock _vCardDescription;
    private Image _vCardIcon;
    private Image _vCardProviderIcon;
    private RadialGradientBrush _VCardIconLabelShadowColor;



    public AppVCard()
    {
      this.DefaultStyleKey = typeof(AppVCard);
    }





    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();

      _vCardLabel = GetTemplateChild(appVCardLabel) as TextBlock;
      _vCardDescription = GetTemplateChild(appVCardDescription) as TextBlock;
      _vCardIcon = GetTemplateChild(appVCardIcon) as Image;
      _vCardProviderIcon = GetTemplateChild(appVCardProviderIcon) as Image;
      _VCardIconLabelShadowColor = GetTemplateChild(appIconLabelShadowColor) as RadialGradientBrush;

      _resolveVisualState();
    }


    private void runApp(object sender, RoutedEventArgs e)
    {

      //XapExplorer.RunApplication();

    }





    //// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc..
    //public static readonly DependencyProperty AppNameProperty =
    //    DependencyProperty.Register("AppName", typeof(string), typeof(AppVCard), new PropertyMetadata("Nazev nevyplnen"));

    ///// <summary>
    ///// uzivatelsky nadpis pro aplikaci, nutno nastavit
    ///// </summary>
    ///// <returns>vrati nadpis/nazev aplikace</returns>
    //public string AppName
    //{
    //  get { return (string)GetValue(AppNameProperty); }
    //  set
    //  {
    //    SetValue(AppNameProperty, value);
    //  }
    //}





    //// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc..
    //public static readonly DependencyProperty AppDescriptionProperty =
    //    DependencyProperty.Register("AppDescription", typeof(string), typeof(AppVCard), new PropertyMetadata("Popis nevyplnen"));

    ///// <summary>
    ///// uzivatelsky popis pro aplikaci, nutno nastavit
    ///// </summary>
    ///// <returns>popis/poznamku k aplikaci</returns>
    //public string AppDescription
    //{
    //  get { return (string)GetValue(AppDescriptionProperty); }
    //  set
    //  {
    //    SetValue(AppDescriptionProperty, value);
    //    // some custom code here
    //  }
    //}






    //// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc..
    //public static readonly DependencyProperty AppIconProperty =
    //  //DependencyProperty.Register("IconProperty", typeof(ImageSource), typeof(AppVCard), new PropertyMetadata(new Uri("/uhul.Controls;component/void.jpg", UriKind.RelativeOrAbsolute)));
    //    DependencyProperty.Register("AppIcon", typeof(ImageSource), typeof(AppVCard), new PropertyMetadata(null));


    ///// <summary>
    ///// umozni nastavit Uri na vlastni obrazek k aplikaci, ktery se bude zobrazovat na vizitce
    ///// </summary>
    ///// <returns>Uri obrazku</returns>
    //public ImageSource AppIcon
    //{
    //  get { return (ImageSource)GetValue(AppIconProperty); }
    //  set
    //  {
    //    SetValue(AppIconProperty, value);
    //  }
    //}





    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc..
    public static readonly DependencyProperty AppInfoProperty =
        DependencyProperty.Register("AppInfo", typeof(AppInfo), typeof(AppVCard), new PropertyMetadata(null, new PropertyChangedCallback(_onAppInfoChanged)));


    /// <summary>
    /// set/get AppInfo
    /// </summary>
    /// <returns>AppInfo</returns>
    public AppInfo AppInfo
    {
      get { return (AppInfo)GetValue(AppInfoProperty); }
      set
      {
        SetValue(AppInfoProperty, value);
        _resolveVisualState();
      }
    }



    private static void _onAppInfoChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      AppVCard tmp = (AppVCard)d;
      tmp._resolveVisualState();
    }





    private void _resolveVisualState()
    {

      if (AppInfo == null)
        return;

      Color descColor;
      Color labelColor;

      Color tmpCol;

      //resolve color theme first...
      if (AppInfo.ThemeColor.Color.A < 40)
        tmpCol = Colors.White;
      else
        tmpCol = AppInfo.ThemeColor.Color;

      int avg;

      avg = (tmpCol.R + tmpCol.G + tmpCol.B /*+ (255 - tmpCol.A)*/) / 3;
      if (avg > 128)
      {
        descColor = Color.FromArgb(255, 0, 0, 0);
        labelColor = Color.FromArgb(255, 255, 255, 255);
      }
      else
      {
        descColor = Color.FromArgb(255, 255, 255, 255);
        labelColor = Color.FromArgb(255, 0, 0, 0);
      }

      //if (_VCardThemeColor != null)
      //  _VCardThemeColor.Background = new SolidColorBrush(AppInfo.ThemeColor);
      
      if (_vCardLabel != null)
      {
        _vCardLabel.Text = AppInfo.Name;
        _vCardLabel.Foreground = new SolidColorBrush(labelColor);
      }

      if (_vCardDescription != null)
      {
        _vCardDescription.Text = AppInfo.Description;
        _vCardDescription.Foreground = new SolidColorBrush(descColor);
      }

      if (_vCardIcon != null)
        _vCardIcon.Source = AppInfo.Icon;

      if (_vCardProviderIcon != null)
        _vCardProviderIcon.Source = AppInfo.ProviderIcon;

      if (_VCardIconLabelShadowColor != null)
      {
        _VCardIconLabelShadowColor.GradientStops[0].Color = descColor;
        _VCardIconLabelShadowColor.GradientStops[1].Color = Color.FromArgb(0, descColor.R, descColor.G, descColor.B);
      }

    }



  }

}
